
package com.thinkgem.jeesite.common.pojo.auth.access.process;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thinkgem.jeesite.common.pojo.auth.access.annotation.AccessLacks;
import com.thinkgem.jeesite.common.pojo.auth.access.type.Relation;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/** 
 * lack访问处理器(类似filter),满足目标的项才会被拦截,否则都通过访问
 *
 */
public class LacksProcess extends AnnoProcess {

	final Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 构造lack处理器
	 * @param accessLacks  lack注解
	 */
	public LacksProcess(AccessLacks accessLacks) {
		logger.debug("process: @AccessLacks");
		if(accessLacks != null){
			// 根据annotation获取方法上绑定的rolelist和officelist.
			setNrlist(accessLacks.role());   //取出annotation中的role值
			setNolist(accessLacks.office()); //取出annotation中的office值
			setRt(accessLacks.relation());   //取出annotation中的relation值
			setDefValue("all");              //设置用来校验的未填项默认值"all"
		}
    }

	public String getDefValue() {
		return defValue;
	}

	public void setDefValue(String defValue) {
		this.defValue = defValue;
	}

	public String[] getNrlist() {
		return nrlist;
	}

	public void setNrlist(String[] nrlist) {
		this.nrlist = nrlist;
	}

	public String[] getNolist() {
		return nolist;
	}

	public void setNolist(String[] nolist) {
		this.nolist = nolist;
	}

	public Relation getRt() {
		return rt;
	}

	public void setRt(Relation rt) {
		this.rt = rt;
	}

	/**
	 * 实现访问处理器的anno检查接口
	 */
	public boolean annoCheck(){
		// 获取当前用户的rolelist,officelist
		List<Role> crlist = UserUtils.getRoleList();
		List<Office> colist = UserUtils.getOfficeList();

		// 单项信息检查注解信息是否和当前用户信息匹配
		boolean nrcheck = andCheck(getNrlist(),crlist,getDefValue());
		boolean nocheck = andCheck(getNolist(),colist,getDefValue());

		// 核对两组值是否符合注解检测规则(默认'与':默认全通过.全部满足条件才会被拦截,有一项不满足则通过检测)
		if(relationCheck(getRt(),nrcheck,nocheck)){
			return false;// 不符合检测,后续直接重定向或提示无权限访问,返回false
		}else{
			return true;// 符合检测,后续将token写入session,并跳出拦截器.
		}
	}

	private String defValue="all";

	private String[] nrlist=null;
	private String[] nolist=null;
	private Relation rt=null;
}
